try:
    import tagging
    HAS_TAGGING = True
except ImportError:
    HAS_TAGGING = False
from django.db import models
from django.dispatch import dispatcher
from django.db.models import signals, permalink
from django.conf import settings
from arcade.signals import process_queue

FEED_CHOICES = (
    ('mochi', 'MochiAds Feed'),
)

class Feed(models.Model):
    """Describes a games feed from a game distributor
    
    """
    feed_type = models.CharField(max_length=255, choices=FEED_CHOICES)
    feed_name = models.TextField()
    feed_location = models.URLField(verify_exists=False)
    #feed_adapter - models.CharField(max_length=255, blank=True, null=True)
        
    class Meta:
        pass
        
    def __unicode__(self):
        return self.feed_name
    
class ArcadeCategory(models.Model):
    name = models.TextField()
    slug = models.SlugField(primary_key=True)
    cat_map = models.TextField(blank=True, null=True)
    
    class Meta:
        verbose_name_plural = "categories"
        
    def __unicode__(self):
        return self.name

class GameManager(models.Manager):
    def get_featured_games(self):
        return self.filter(featured=True, downloaded=True).order_by('-site_added_date')
        
    def get_top_games(self):
        return self.filter(downloaded=True).order_by('-game_plays')
        
    def get_random_games(self):
        return self.filter(downloaded=True).order_by('?')
    
class Game(models.Model):
    objects = GameManager()
    
    uuid = models.CharField(max_length=255, editable=False)
    tag = models.CharField(primary_key=True, max_length=16)
    
    name = models.CharField(max_length=255)
    slug = models.SlugField()
    description = models.TextField(blank=True, null=True)

    author = models.CharField(max_length=255)
    author_link = models.URLField(verify_exists=False, editable=False)

    width = models.PositiveIntegerField()
    height = models.PositiveIntegerField()
    
    thumbnail_url = models.CharField(max_length=255)
    
    updated = models.DateTimeField()
    
    control_scheme = models.TextField(blank=True, null=True, help_text="JSON encoded controls")
    instructions = models.TextField(blank=True, null=True)
    key_mappings = models.TextField(blank=True, null=True) 
    
    leaderboard_enabled = models.BooleanField(default=False, editable=False)
    rating = models.CharField(max_length=255, help_text="ESRB Rating")
    game_categories = models.TextField(editable=False)
    game_tags = models.TextField(editable=False)
    
    swf_url = models.URLField(verify_exists=False, editable=False)
    
    created = models.DateTimeField(editable=False)
    feed_approval_created = models.DateTimeField(editable=False)
    
    categories = models.ManyToManyField(ArcadeCategory, blank=True, null=True)

    download = models.BooleanField(default=False)
    downloaded = models.BooleanField(default=False, editable=False)
    
    queued = models.BooleanField(default=True, editable=False)
    featured = models.BooleanField(default=False)
    site_added_date = models.DateTimeField(auto_now_add=True, editable=False)
    game_plays = models.PositiveIntegerField(default=0, editable=False)
    
    class Meta:
        ordering = ('-feed_approval_created',)
  
    def get_absolute_url(self):
        return ('game_detail', (), {'slug':self.slug})
    get_absolute_url = permalink(get_absolute_url)
    
    def add_play(self):
        self.game_plays = self.game_plays + 1
        self.save()    
    
    def id(self):
        return self.tag    
        
    def __unicode__(self):
        return self.name
    
    def get_swf_url(self):
        if self.downloaded:
            return settings.ARCADE_STATIC_URL + "%s/%s" % (self.slug, self.swf_url.rsplit('/',1).pop())
        else:
            return self.swf_url
    
    def get_thumbnail_url(self):
        if self.downloaded and not self.tag in self.thumbnail_url and '_thumbnail' in self.thumbnail_url:
            return settings.ARCADE_STATIC_URL + "%s/%s" % (self.slug, self.thumbnail_url.rsplit('/',1).pop())
        else:
            return self.thumbnail_url
    
    def short_description(self):
        return self.description[:100]
    short_description.short_description = 'Game Description'
    
dispatcher.connect(process_queue, signal=signals.post_save, sender=Game)

if HAS_TAGGING:    
    tagging.register(Game)
    
class DownloadQueue(models.Model):
    game = models.ForeignKey(Game)
    created = models.DateTimeField(auto_now_add=True)
    download = models.BooleanField(default=False)
 
    def __unicode__(self):
        return self.game.__unicode__()
    
    def short_description(self):
        return self.game.short_description()
    short_description.short_description = 'Game Description'
    
    def downloaded(self):
        return self.game.downloaded
    
    def featured(self):
        return self.game.featured
        
    def feed_approval_created(self):
        return self.game.feed_approval_created
    